package light.mvc.controller.patient;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.admin.User;
import light.mvc.page.base.Grid;
import light.mvc.page.base.PageFilter;
import light.mvc.page.patient.Register;
import light.mvc.service.admin.UserServiceI;
import light.mvc.service.patient.RegisterSeqServiceI;
import light.mvc.service.patient.ReserveRegisterServiceI;
import light.mvc.service.patient.SiteRegisterServiceI;
import light.mvc.utils.TimeUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/registerSeq")
public class RegisterSeqController extends BaseController {

	@Autowired
	private RegisterSeqServiceI registerSeqService;
	
	@Autowired
	private SiteRegisterServiceI siteRegisterService;
	
	@Autowired
	private ReserveRegisterServiceI reserveRegisterService;
	
	@Autowired
	private UserServiceI userService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		Date timeSegment = null;
		try {
			timeSegment = TimeUtil.getNowTimeSegment();
		} catch (Exception e) {
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("errorCode", "1");
			return "/register/registerSeq";
		}
		request.setAttribute("msg", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timeSegment));
		request.setAttribute("errorCode", "0");
		return "/register/registerSeq";
	}
	
	@RequestMapping("/doctorDataGrid")
	@ResponseBody
	public Grid doctorDataGrid(User doctor, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(userService.doctorGrid(doctor, ph));
		grid.setTotal(userService.doctorGridCount(doctor, ph));
		return grid;
	}

	@RequestMapping("/detailPage")
	public String addPage(HttpServletRequest request,Integer doctorId,String timeSegmentStr) {
		Date timeSegment = TimeUtil.str2Date(timeSegmentStr);
		
		List<Register> registers = registerSeqService.getPatientsByDoctorId(doctorId, timeSegment);
		request.setAttribute("registers", registers);
		return "/register/registerSeqDetail";
	}
}
